<?php

require_once("Dimension3.php");
require_once("Dimension2.php");

/** Description of Dimension2Manager
 *
 * @author yahn
 */
class Dimension2Manager {

    private $_db;

    public function __construct($db) {
        $this->setDb($db);
    }

    public function add(Dimension2 $d2) {
        $q = $this->_db->prepare('INSERT INTO Dimension2 VALUES (:refD1,:refD2, :titre, :descr)');
        $q->bindValue(':refD1', $d2->getDimension1());
        $q->bindValue(':refD2', $d2->getDimension2());
        $q->bindValue(':titre', $d2->getTitre());
        $q->bindValue(':descr', $d2->getDescr());
        $q->execute();
    }

    public function deleteAll(Dimension2 $d2) {
        $exec = $this->_db->exec("DELETE FROM Dimension2 WHERE refD2 = '" . $d2->getRefD2() . "'");
        return $exec;
    }

    public function delete(Dimension2 $d2) {
        $exec = $this->_db->exec("DELETE FROM Dimension2 WHERE refD2 = '" . $d2->getRefD2() . "' and refD1= '" . $d2->getRefD1() . "'");
        return $exec;
    }

    /**
     * Compte le nombre de Dimension2 de Dimension1
     * @param type $refD1
     * @return type 
     */
    public function count(Dimension2 $d2) {
        return $this->_db->query("SELECT COUNT(*) FROM Dimension2 where refD1='" . $d2->getDimension1()->getRefD1() . "'")->fetchColumn();
    }

    /**
     *
     * @param type $refD1 référence Dimension1
     * @param type $refD2 référence Dimension2
     * @return \Dimension2 
     */
    public static function get($refD1, $refD2) {
        $pdo = PDO2::getInstance();
        if (!isset($ref2)) {
            
            $requete = $pdo->query("SELECT refD1, refD2, titre, descr FROM Dimension2 WHERE refD1 = '" . $refD1 . "' and refD2 = '" . $refD2 . "'");
            $donnees = $requete->fetch(PDO::FETCH_OBJ);
            $managerD1 = new Dimension1Manager($this->$_db);
            $dimension1 = $managerD1->get($donnees->refD1);
            $dimension2 = new Dimension2($dimension1, $donnees->refD1, $donnees->titre, $donnees->descr);
            $dimension2->addDimension3(self::getDimension3($dimension2));
            return $dimension2; 
        } else {
            
            $requete = $pdo->query("SELECT refD1, refD2, titre, descr FROM Dimension2 WHERE refD1 = '". $refD1."'");
            $tableauDimension2;
            while($donnees = $requete->fetch(PDO::FETCH_OBJ)) {
                $tableauDimension2[] = $donnees;
            }
            
            return $tableauDimension2;
            
        }
    }

    public static function getDimension3(Dimension2 $d2) {
        $pdo = PDO2::getInstance();
        $q = $pdo->prepare("SELECT d3.refD2, d3.refD1, niveau, d3.descr FROM Dimension2 d2, Dimension3 d3 where d2.refD1 = d3.refD1 and d2.refD2 = d3.refD2 and d2.refD2 = :refD2 and d2.refD1 = :refD1");
        $q->bindValue(':refD2', $d2->getDimension2());
        $q->bindValue(':refD1', $d2->getDimension1()->getRefD1());
        $q->execute();
        //tableau qui va contenir les résultats
        $lesDimensions3 = array();

        while ($lignes = $q->fetch(PDO::FETCH_OBJ)) {
            $niveau = $lignes->niveau;
            $descr = $lignes->descr;

            if ($descr <> "Non applicable.") {
                $d3 = new Dimension3($d2->getDimension1(), $d2, $niveau, $descr);
                $lesDimensions3[] = $d3;
            }
        }
        return $lesDimensions3;
    }
   
    public function setDb(PDO $db) {
        $this->_db = $db;
    }

}

?>
